<template>
	<view class="my-box">
		<view class="">
			<!-- #ifdef MP-WEIXIN -->
			<my-wxChart></my-wxChart>
			<!-- #endif -->
			<!-- #ifdef MP-ALIPAY -->
			<my-zfb></my-zfb>
			<!-- #endif -->
		</view>
		<view class="my-top" :style="{background:bgcolor}">
			<!-- 头像和手机号 -->
			<view class="auther-box">
				<view class="auther-left">
					<!-- #ifdef MP-ALIPAY -->
					<up-avatar :src="userData.headerimage"></up-avatar>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<image src="@/static/image/logo.png" mode="" class="wx-image"></image>
					<!-- #endif -->
				</view>
				<view class="auther-right">
					<!-- #ifdef MP-WEIXIN -->
					<view class="authonr-user" style="color: #171717;" v-if="token">{{userData.mobile}}</view>
					<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="avatar-auth" v-else>登录/注册</button>
					<!-- #endif -->
					<!-- #ifdef MP-ALIPAY -->
					<view class="authonr-user" style="color: #ffffff;" v-if="token">{{userData.mobile}}</view>
					<button open-type="getAuthorize" scope="phoneNumber" @getAuthorize="getPhoneNumber" @error="handleAuthError"
						class="avatar-auth" v-else>登录/注册</button>
					<view class="authonr-user" style="margin-top: 10rpx;font-size: 36rpx;" @click="myEwm" v-if="token">我的门店码
					</view>
					<!-- #endif -->
				</view>
			</view>
			<!-- 重量和余额 -->
			<view class="weight-yu">
				<view class="weight-l">
					<text>累计回收重量(kg)</text>
					<text>{{drawData.zl}}</text>
				</view>
				<view class="yu-r" @click="moneyClick">
					<text>结算金额(元)</text>
					<text>{{drawData.ye}}</text>
				</view>
			</view>
		</view>

		<!-- 常用功能 -->
		<view class="modul-box">
			<view class="modul-title">常用功能</view>
			<view class="modul-list">
				<view class="modul-item" v-for="(item,index) in tabsList" :key="index" @click="applyClick(item)">
					<up-icon :name="item.icon" color="#171717" size="28"></up-icon>
					<text>{{item.name}}</text>
				</view>
			</view>
		</view>

		<view class="">
			<up-popup :show="popupShow" mode="center" :round="5" @close="popupShow = false">
				<view class="qrcode-popup">
					<up-qrcode :size="200" :val="qrcodeValue" showLoading loadingText="loading..." ref="qrcode"></up-qrcode>
				</view>
				<up-button type="default" color="#3296FA" text="保存二维码" @click="saveQRCodeToAlbum"></up-button>
			</up-popup>
		</view>

	</view>
</template>
<script setup>
	import {
		ref,
		onMounted,
		onBeforeMount
	} from 'vue';
	import {
		onLoad,
		onPullDownRefresh
	} from "@dcloudio/uni-app"
	import {
		getLogin,
		getUserDraw
	} from '@/api/http.js'
	import {
		getLoginRegister,
		getUserIndex
	} from '@/api/zfb.js'
	// #ifdef MP-ALIPAY
	const bgcolor = ref('#3296FA')
	// #endif
	// #ifdef MP-WEIXIN
	const bgcolor = ref('transparent')
	// #endif
	// 手机号授权
	const userData = ref(uni.getStorageSync('userData'))
	const token = ref(uni.getStorageSync('token'))
	const getPhoneNumber = async (e) => {
		// #ifdef MP-WEIXIN
		uni.login({
			provider: 'weixin', //使用微信登录
			success: async (loginRes) => {
				getLogin({
					logincode: loginRes.code,
					iv: e.detail.iv,
					encryptedData: e.detail.encryptedData,
					// originalType: 1,
				}).then((res) => {
					if (res.code != 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						return
					}
					let result = JSON.parse(res.data)

					userData.value = result
					token.value = result.token
					uni.setStorageSync('token', result.token)
					uni.setStorageSync('userData', result)
					uni.showToast({
						title: '授权成功',
						icon: 'none'
					})
				})
			}
		});
		// #endif
		// #ifdef MP-ALIPAY
		my.getPhoneNumber({
			success: (phoneNum) => {
				my.getAuthCode({
					scopes: 'auth_user',
					success: (authRes) => {
						getLoginRegister({
							code: authRes.authCode,
							encryptedData: JSON.parse(phoneNum.response).response,
							openkey: JSON.parse(phoneNum.response).response,
						}).then((res) => {
							if (res.code != 200) {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
								return
							}
							let result = JSON.parse(res.data)
							console.log(result);
							userData.value = result
							token.value = result.token
							uni.setStorageSync('token', result.token)
							uni.setStorageSync('userData', result)
							uni.showToast({
								title: '授权成功',
								icon: 'none'
							})
							// #ifdef MP-WEIXIN
							getDraw()
							// #endif
							// #ifdef MP-ALIPAY
							getUserInfo()
							// #endif
						})
					}
				})
			}
		})
		// #endif
	}

	// 累计回收重量和结算金额
	onMounted(() => {
		if (token.value) {
			// #ifdef MP-WEIXIN
			getDraw()
			// #endif
			// #ifdef MP-ALIPAY
			getUserInfo()
			// #endif
		}
	})
	const drawData = ref({
		ye: 0.00,
		zl: 0.00
	})
	const getDraw = async () => {
		const res = await getUserDraw()
		if (res.code != 200) {
			uni.showToast({
				title: res.message,
				icon: 'none'
			})
			return
		}
		drawData.value.ye = res.yue
		drawData.value.zl = res.zhongliang
	}
	// 获取个人信息
	const userInfoData = ref({})
	const getUserInfo = async () => {
		const res = await getUserIndex()
		userInfoData.value = JSON.parse(res.user_info)
		console.log(userInfoData.value, '个人信息123');
		if (userInfoData.value.enums == 1) {
			// 1个人
			// 常用功能
			tabsList.value = [{
				id: 1,
				name: '申请入驻',
				icon: 'account',
				url: '/subPackages/my-pages/apply-for/apply-for'
			}]
		} else if (userInfoData.value.enums == 2) {
			// 司机2
			tabsList.value = [{
				id: 4,
				name: '运力管理',
				icon: 'car',
				url: '/subPackages/transportation/driver-list/driver-list'
			}]
		} else {
			// 3门店
			tabsList.value = [{
				id: 3,
				name: '门店管理',
				icon: 'calendar',
				url: '/subPackages/store-manage/store-list/store-list'
			}]
		}
	}
	// 下拉刷新
	onPullDownRefresh(() => {
		// #ifdef MP-WEIXIN
		getDraw()
		// #endif
		// #ifdef MP-ALIPAY
		getUserInfo()
		// #endif
		setTimeout(() => {
			uni.stopPullDownRefresh()
		}, 1000)
	})
	// 常用功能
	const tabsList = ref([
		// #ifdef MP-WEIXIN
		{
			id: 2,
			name: '订单管理',
			icon: 'order',
			url: '/subPackages/recycle/recycling-list/recycling-list'
		}
		// #endif
	])
	// 点击常用功能
	const applyClick = (item) => {
		uni.navigateTo({
			url: item.url
		})
	}
	// 点击金额
	const moneyClick = () => {
		// #ifdef MP-WEIXIN
		uni.navigateTo({
			url: '/subPackages/my-pages/my-money/my-money'
		})
		// #endif
	}
	// 二维码
	const popupShow = ref(false)
	const qrcodeValue = ref('')
	const qrcode = ref(null);
	const myEwm = () => {
		popupShow.value = true;
		qrcodeValue.value = 'https://yzsapi.qdxnhb.com/?y=appid/wx9294808d72449aa5/store_id/' + uni.getStorageSync(
			'userData').productId 
	}

	// 保存二维码到相册
	// 保存二维码到相册
	const saveQRCodeToAlbum = async () => {
		return new Promise((resolve, reject) => {
			my.saveImage({
				url: qrcode.value.result,
				success: () => {
					my.showToast({
						content: '保存成功',
						type: 'success',
					});
					resolve();
				},
				fail: (err) => {
					my.showToast({
						content: '保存失败',
						type: 'fail',
					});
					reject(err);
				},
			});
		});
	}
</script>
<style lang="scss" scoped>
	.my-box {
		background-color: #f7f7f7;
		height: 100vh;
	}

	.my-top {
		position: relative;
		z-index: 999;
		height: 381rpx;
		padding: 60rpx 30rpx;
		box-sizing: border-box;

		.auther-box {
			display: flex;
			align-items: center;

			.auther-left {
				margin-right: 20rpx;
				width: 135rpx;
				height: 135rpx;

				::v-deep .u-avatar__image--circle,
				::v-deep .u-avatar--circle {
					width: 135rpx !important;
					height: 135rpx !important;
				}

				.wx-image {
					width: 135rpx;
					height: 135rpx;
				}
			}

			.auther-right {
				font-size: 40rpx;
				font-weight: 700;

				.authonr-user {
					background-color: transparent;
					border: none;
					outline: none;
				}


				.avatar-auth {
					font-size: 40rpx;
					padding: 0 !important;
					border: none;
					background-color: transparent;

					&::after {
						border: none;
					}
				}
			}
		}

		.weight-yu {
			display: flex;
			margin-top: 50rpx;
			border-radius: 20rpx;
			padding: 30rpx 0;
			background-color: #ffffff;

			.weight-l,
			.yu-r {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				color: #171717;

				text {
					font-size: 32rpx;
					font-weight: 700;
					color: #171717;

					&:last-child {
						font-size: 40rpx;
						margin-top: 20rpx;
					}
				}
			}
		}
	}

	.modul-box {
		margin: 70rpx 30rpx 30rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 20rpx 30rpx;

		.modul-title {
			color: #171717;
			font-size: 28rpx;
			font-weight: 700;
		}

		.modul-list {
			display: flex;
			flex-wrap: wrap;
			margin-top: 22rpx;

			.modul-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				width: 25%;

				text {
					font-size: 26rpx;
					color: #171717;
					margin-top: 10rpx;
				}
			}
		}
	}

	.qrcode-popup {
		// width: 140px;
		// height: 140px;
		padding: 40rpx;
	}
</style>